package Teacher.Examination;

import java.awt.*;
import java.awt.event.*;
import java.sql.*;

import javax.swing.*;

import Database.*;
import Exception.*;
import General.*;

public class PanelExamination extends ManagementPanel
{
	private JLabel labelCourse, labelGroup, labelDate, labelType, labelCreator, labelCourseShow, labelGroupShow, labelDateShow, labelTypeShow, labelCreatorShow;
	private JCheckBox boxSecondSess;
	
	public PanelExamination(CallDB callDB, Connection connection) throws CheckDataException, FormatScrollPaneException
	{
		super(callDB, connection); //Pass through the super constructor
		
		//Suppress the default descript JTable
		scrolltableDescript.setVisible(false);
		scrolltableDescript.setEnabled(false);
		
		enableButton(false);
		createComponent();
		showComponent();
	}
	
	public void showDetail(boolean clean) throws CheckDataException, LoadDriverException
	{
		if(clean == false)
		{
			int selectedRow = tableLeft.convertRowIndexToModel(tableLeft.getSelectedRow());
			MyTableModel resultModel = new ExaminationMgmt(connection).selectAllInformationsAbout((int)GeneralInformation.getExamModel().getValueAt(selectedRow, 2));

			labelCourseShow.setText((String)resultModel.getValueAt(0, 3));
			labelGroupShow.setText(resultModel.getValueAt(0, 4) + " " + (String)resultModel.getValueAt(0, 5) + " " + (String)resultModel.getValueAt(0, 6));
			labelDateShow.setText((String)resultModel.getValueAt(0, 1));
			labelTypeShow.setText((String)resultModel.getValueAt(0, 2));
			labelCreatorShow.setText((String)resultModel.getValueAt(0, 0));
		}
		else
			clearLabel();
		
		validate();
	}
	
	public void enableButton(boolean state)
	{
		getButtonModify().setEnabled(state);
		getButtonDelete().setEnabled(state);
	}

	private class MyMouseListener implements MouseListener
	{
		public void mouseClicked(MouseEvent arg0)
		{
			try
			{
				if(tableLeft.getSelectedRow() != -1)
				{
					enableButton(true);
					showDetail(false);
				}
			}
			catch(CheckDataException | LoadDriverException error)
			{
				TreatError.showError(error);
			}
		}
		
		public void mouseEntered(MouseEvent arg0){}
		public void mouseExited(MouseEvent arg0){}
		public void mousePressed(MouseEvent arg0){}
		public void mouseReleased(MouseEvent arg0){}
	}
	
	private class MyActionListener implements ActionListener
	{
		public void actionPerformed(ActionEvent arg0)
		{
			try
			{
				if(arg0.getSource() == getButtonDelete())
					deleteExamination();
				else if(arg0.getSource() == boxSecondSess)
				{
					enableButton(false);
					showDetail(true);
					if(boxSecondSess.isSelected())
						GeneralInformation.createExamSecondSessModel(connection);
					else
						GeneralInformation.createExamModel(connection);
	
					GeneralMethod.setLeftModel(GeneralInformation.getExamModel());
					refreshSuperLeftTable();
				}
			}
			catch(NumberFormatException | InsertionDataException | CheckDataException | EraseDataException | FormatScrollPaneException | ModificationDBException | LoadDriverException error)
			{
				TreatError.showError(error);
			}
		}
	}
	
	public void deleteExamination() throws NumberFormatException, InsertionDataException, CheckDataException, EraseDataException, FormatScrollPaneException, ModificationDBException, LoadDriverException
	{
		int selectedRow = tableLeft.convertRowIndexToModel(tableLeft.getSelectedRow()); //Get the index of the selected row		
		new ExaminationMgmt(connection).deleteElement(Integer.parseInt(GeneralInformation.getExamModel().getValueAt(selectedRow, 2).toString())); //Delete the selected element

		GeneralInformation.createExamModel(connection);
		GeneralMethod.setLeftModel(GeneralInformation.getExamModel());
		showDetail(true);

		refreshSuperLeftTable();
		enableButton(false);
	}
	
	public void clearLabel()
	{
		labelCourseShow.setText("");
		labelGroupShow.setText("");
		labelDateShow.setText("");
		labelTypeShow.setText("");
		labelCreatorShow.setText("");
	}
	
	public void createComponent()
	{
		MyMouseListener mouseListener = new MyMouseListener();
		MyActionListener actionListener = new MyActionListener();
		
		tableLeft.addMouseListener(mouseListener);
		
		labelCourse = new JLabel("Course");
		labelCourse.setHorizontalAlignment(SwingConstants.LEFT);
		labelCourse.setFont(new Font("Constantia", Font.BOLD | Font.ITALIC, 18));
		labelCourse.setForeground(Color.WHITE);

		labelGroup = new JLabel("Group");
		labelGroup.setHorizontalAlignment(SwingConstants.LEFT);
		labelGroup.setFont(new Font("Constantia", Font.BOLD | Font.ITALIC, 18));
		labelGroup.setForeground(Color.WHITE);

		labelDate = new JLabel("Date");
		labelDate.setHorizontalAlignment(SwingConstants.LEFT);
		labelDate.setFont(new Font("Constantia", Font.BOLD | Font.ITALIC, 18));
		labelDate.setForeground(Color.WHITE);

		labelType = new JLabel("Type");
		labelType.setHorizontalAlignment(SwingConstants.LEFT);
		labelType.setFont(new Font("Constantia", Font.BOLD | Font.ITALIC, 18));
		labelType.setForeground(Color.WHITE);

		labelCreator = new JLabel("Creator");
		labelCreator.setHorizontalAlignment(SwingConstants.LEFT);
		labelCreator.setFont(new Font("Constantia", Font.BOLD | Font.ITALIC, 18));
		labelCreator.setForeground(Color.WHITE);

		boxSecondSess = new JCheckBox("2e Sess");
		boxSecondSess.setName("boxSecondSess");
		
		labelCourseShow = new JLabel("");
		labelCourseShow.setForeground(Color.WHITE);
		labelGroupShow = new JLabel("");
		labelGroupShow.setForeground(Color.WHITE);
		labelDateShow = new JLabel("");
		labelDateShow.setForeground(Color.WHITE);
		labelTypeShow = new JLabel("");
		labelTypeShow.setForeground(Color.WHITE);
		labelCreatorShow = new JLabel("");
		labelCreatorShow.setForeground(Color.WHITE);
		
		boxSecondSess.addActionListener(actionListener);
		getButtonDelete().addActionListener(actionListener);
	}
	
	public void showComponent()
	{
		labelCourse.setBounds(446, 57, 90, 25);
		labelGroup.setBounds(446, 117, 90, 25);
		labelDate.setBounds(446, 177, 90, 25);
		labelType.setBounds(446, 237, 90, 25);
		labelCreator.setBounds(446, 297, 90, 25);
		labelCourseShow.setBounds(538, 57, 200, 25);
		labelGroupShow.setBounds(538, 117, 200, 25);
		labelDateShow.setBounds(538, 177, 200, 25);
		labelTypeShow.setBounds(538, 237, 200, 25);
		labelCreatorShow.setBounds(538, 297, 200, 25);
		boxSecondSess.setBounds(50, 357, 107, 25);
		
		this.add(labelCourse);
		this.add(labelGroup);
		this.add(labelDate);
		this.add(labelType);
		this.add(labelCreator);
		this.add(labelCourseShow);
		this.add(labelGroupShow);
		this.add(labelDateShow);
		this.add(labelTypeShow);
		this.add(labelCreatorShow);
		this.add(boxSecondSess);
		
		GraphicsSetting.setBackground(this, "image/Bg-Main.jpg", 0, 0, 800, 600);
	}
	
	public JCheckBox getBoxSecondSess(){return boxSecondSess;}
	public JTable getTableLeft(){return tableLeft;}
}